package search;

/**
 * Created with IntelliJ IDEA.
 * ClassName: TwoSearch
 * Package: search
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-08-16
 * Time: 9:57
 */

//二分查找 前提是有序的
public class TwoSearch {
    public static void main(String[] args) {
        //数组对破分开 比较一半
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};

        int tmp = 7;

        //定义收尾索引
        int head = 0;
        int end = arr.length - 1;

        boolean isFlag = false;
        while (head <= end) {

            int mid = (head + end) / 2;
            if (arr[mid] == tmp) {
                System.out.println("找到了: " + arr[mid]);
                isFlag = true;
                break;
            } else if (arr[mid] < tmp) {
                head = mid + 1;
            } else {
                end = mid - 1;
            }
        }

        if (!isFlag) {
            System.out.println("没找到");
        }

    }
}
